package com.waiter.moximoxi.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.waiter.moximoxi.R;
import com.waiter.moximoxi.bean.MainMessageBean;


import java.text.DecimalFormat;

import butterknife.OnClick;
import cn.yhong.aframework.dialog.BaseDialog;
import cn.yhong.aframework.listener.OnCallbackListener;

/**
 * Created by 17639 on 2020/10/14.
 */

public class MessageDialog extends BaseDialog implements View.OnClickListener {

    private TextView table_num, table_time, table_price, table_people_count, calculation_time, table_state, table_confirm;

    private long baseTimer;
    private MainMessageBean mainMessageBean;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (0 == baseTimer) {
                baseTimer = SystemClock.elapsedRealtime();
            }

            int time = (int) ((SystemClock.elapsedRealtime() - baseTimer) / 1000);
            String hh = new DecimalFormat("00").format(time / 3600);
            String mm = new DecimalFormat("00").format(time % 3600 / 60);
            String ss = new DecimalFormat("00").format(time % 60);
            String timeFormat = new String(hh + ":" + mm + ":" + ss);

            if (calculation_time != null) {
                calculation_time.setText(timeFormat);
            }

            handler.sendMessageDelayed(Message.obtain(handler, 0x0), 1000);
        }
    };

    public MessageDialog(Context context, OnCallbackListener onCallbackListener) {
        super(context, onCallbackListener);
    }

    @Override
    public Dialog initDialog(Context context) {
        View view = View.inflate(context, R.layout.dialog_message, null);
        table_num = view.findViewById(R.id.table_num);
        table_time = view.findViewById(R.id.table_time);
        table_price = view.findViewById(R.id.table_price);
        table_people_count = view.findViewById(R.id.table_people_count);
        calculation_time = view.findViewById(R.id.calculation_time);
        table_state = view.findViewById(R.id.table_state);
        table_confirm = view.findViewById(R.id.table_confirm);

        table_confirm.setOnClickListener(this);

        Dialog dialog = initMatchWrapDialog(view, context, Gravity.CENTER, android.R.style.Animation_InputMethod);

        dialog.setCanceledOnTouchOutside(false);

        return dialog;
    }

    public void showMeaageDiaog(MainMessageBean mainMessageBean) {
        this.mainMessageBean = mainMessageBean;
        table_num.setText(mainMessageBean.num);
        table_time.setText(mainMessageBean.order_time);
        table_price.setText("消费：" + mainMessageBean.price);
        table_people_count.setText(mainMessageBean.people_count);
        table_state.setText(mainMessageBean.state);

        handler.sendEmptyMessage(0);
        showDialog();
    }

    @OnClick({R.id.table_confirm})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.table_confirm:
                onCallbackListener.onCallback(0, mainMessageBean);
                dismissDialog();
                baseTimer = 0;
                calculation_time.setText("00:00:00");
                if (handler != null) {
                    handler.removeCallbacksAndMessages(null);
//                    handler = null;
                }
                break;
        }
    }

    /**
     * dialog是否显示
     * @return
     */
    public boolean isShowing(){
       return dialog.isShowing();
    }

    public void closeHandle() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }

}
